/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.jfonia.view.panels;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JLayeredPane;
import org.jfonia.assets.fonts.Fughetta;
import org.jfonia.connect5.basics.BasicValueNode;
import org.jfonia.connect5.basics.MutableValueNode;
import org.jfonia.connect5.basics.Observer;
import org.jfonia.connect5.relations.Equal;
import org.jfonia.view.main.LeadSheet;

/**
 *
 * @author jannes
 */
public class SimpleLeadSheetPanel extends JLayeredPane{
        private MutableValueNode<Double> xNode;
        private MutableValueNode<Double> yNode;
        private MutableValueNode<Double> widthNode;
        private MutableValueNode<Double> heightNode;

        private ViewPanel viewPanel;
        private ViewLayerPanel viewLayerPanel;
        private EditorPanel editorPanel;
        private PlayPanel playPanel;

        public SimpleLeadSheetPanel(MutableValueNode<Double> xNode, MutableValueNode<Double> yNode,
                              MutableValueNode<Double> widthNode, MutableValueNode<Double> heightNode)
        {
                super();
                this.xNode = new BasicValueNode<Double>(0.0);
                //Later an offset can be added here
                new Equal(xNode, this.xNode);
                this.yNode = new BasicValueNode<Double>(0.0);
                //Later an offset can be added here
                new Equal(yNode, this.yNode);
                this.widthNode = new BasicValueNode<Double>(0.0);
                //Later an offset can be added here
                new Equal(widthNode, this.widthNode);
                this.heightNode = new BasicValueNode<Double>(0.0);
                //Later an offset can be added here
                new Equal(heightNode, this.heightNode);
                init();
        }

        private void init()
        {
                setBounds(xNode.getValue().intValue(), yNode.getValue().intValue(),
                        widthNode.getValue().intValue(), heightNode.getValue().intValue());
                setPreferredSize(new Dimension(widthNode.getValue().intValue(),
                        heightNode.getValue().intValue()));

                xNode.addObserver( new Observer() {
			public void onNotify(Object source)
			{
				setLocation(xNode.getValue().intValue(), getY());
			}
		});
                yNode.addObserver( new Observer() {
			public void onNotify(Object source)
			{
				setLocation(getX(), yNode.getValue().intValue());
			}
		});
                widthNode.addObserver( new Observer() {
			public void onNotify(Object source)
			{
				setBounds(getX(), getY(),
                                          widthNode.getValue().intValue(), getHeight());
                                setPreferredSize(new Dimension(widthNode.getValue().intValue(), getHeight()));
                                //setMinimumSize(new Dimension(widthNode.getValue().intValue(), getHeight()));
			}
		});
                heightNode.addObserver( new Observer() {
			public void onNotify(Object source)
			{
				setBounds(getX(), getY(),
                                          getWidth(), heightNode.getValue().intValue());
                                setPreferredSize(new Dimension(getWidth(), heightNode.getValue().intValue()));
                                //setMinimumSize(new Dimension(widthNode.getValue().intValue(), getHeight()));
			}
		});
        }

        private void initPanels()
        {
                removeAll();
                viewLayerPanel = new ViewLayerPanel(this.xNode, this.yNode, this.widthNode, this.heightNode);
                viewPanel = new ViewPanel(viewLayerPanel, this.xNode, this.yNode, this.widthNode, this.heightNode);
                add(viewLayerPanel, 0);
                add(viewPanel,1);
                System.out.println("initpanels done");
        }

        @Override
        public boolean hasFocus()
        {
                return /*super.hasFocus() || */viewPanel.hasFocus();
        }

        public double getX(int tick)
        {
                return viewPanel.getX(tick);
        }

        public void setLeadSheet(LeadSheet leadSheet)
        {
                initPanels();
                viewPanel.setLeadSheet(leadSheet);
        }

        //TODO: temporary, properties path needs to be saved elsewhere (now in Fughetta.class)
        @Override
        protected void paintComponent(Graphics g)
        {
                super.paintComponent(g);
                Graphics2D g2 = (Graphics2D) g;
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                    RenderingHints.VALUE_ANTIALIAS_ON);
                try
                {
                        Fughetta.init(g2);
                }
                catch (Exception e)
                {
                        Logger.getLogger(LeadSheetPanel.class.getName()).log(Level.SEVERE, null, e);
                }
        }

        @Override
        public boolean isOptimizedDrawingEnabled()
        {
                return false;
        }
}
